package com.changyou.kf.core.model;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Schema(description = "分页实体")
@Data
public class PageEntity {

    private static final int MAX_PAGE_SIZE = 1000;

    private static final int DEFAULT_PAGE_SIZE = 20;

    @Parameter(description = "当前页码,默认值:1", required = false, example = "1")
    private Integer page = 1;

    @Parameter(description = "每页记录数,默认值:" + DEFAULT_PAGE_SIZE, required = false,
            example = "" + DEFAULT_PAGE_SIZE)
    private Integer size = DEFAULT_PAGE_SIZE;

    public PageEntity() {
    }

    public PageEntity(Integer page, Integer size) {
        this.page = page;
        this.size = size;
        if (this.page == null || this.page < 1) {
            this.page = 1;
        }
        if (this.size == null || this.size < 1) {
            this.size = DEFAULT_PAGE_SIZE;
        }
        if (this.size > MAX_PAGE_SIZE) {
            this.size = MAX_PAGE_SIZE;
        }
    }

    @Schema(hidden = true)
    public Integer getSkip() {
        return (getPage() - 1) * getSize();
    }

    public Integer getPage() {
        if (this.page == null || this.page < 1) {
            this.page = 1;
        }
        return this.page;
    }

    public Integer getSize() {
        if (this.size == null) {
            this.size = DEFAULT_PAGE_SIZE;
        }
        if (this.size > MAX_PAGE_SIZE) {
            this.size = MAX_PAGE_SIZE;
        }
        return this.size;
    }

    @Override
    public String toString() {
        return String.format("PageEntity{page=%d,size=%d}",
                this.getPage(), this.getSize());
    }

    public static PageEntity of(Integer page, Integer size) {
        return new PageEntity(page, size);
    }
}
